package com.linxianqin.springbootVue.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.constant.UserConstants;
import com.linxianqin.springbootVue.domain.convert.SysOperLogConvert;
import com.linxianqin.springbootVue.domain.convert.SysPostConvert;
import com.linxianqin.springbootVue.domain.dto.SysPostDTO;
import com.linxianqin.springbootVue.domain.dto.SysUserDTO;
import com.linxianqin.springbootVue.domain.entity.SysOperLog;
import com.linxianqin.springbootVue.domain.entity.SysPost;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.domain.query.SysPostQuery;
import com.linxianqin.springbootVue.domain.vo.SysOperLogExcel;
import com.linxianqin.springbootVue.domain.vo.SysPostExcel;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.SysPostService;
import com.linxianqin.springbootVue.utils.SecurityUtils;
import com.linxianqin.springbootVue.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.ap.internal.model.assignment.UpdateWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lxq
 * @version 1.0
 * @description: 岗位信息操作处理
 * @date 2020/12/16 9:34
 */
@Slf4j
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    private final SysPostService postService;
    public SysPostController(SysPostService postService){
        this.postService=postService;
    }

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:post:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPostQuery query)
    {
        log.info("查询的信息：{}",query);
        //构建分页bean
        Page<SysPostDTO> page = buildPage();
        //查询分页数据
        Page<SysPostDTO> dtoPage = postService.selectPostPage(page, query);
        return getDataTable(dtoPage);
    }

//    @SysLog(title = "岗位管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@ss.hasPermi('system:post:export')")
//    @GetMapping("/export")
//    public AjaxResult export()
//    {
//        //查询数据库，查询所有
//        List<SysPost> list = postService.list();
//        //构建Excel工具包
//        ExcelUtil<SysPostExcel> util = new ExcelUtil<>(SysPostExcel.class);
//        //转化实体
//        List<SysPostExcel> excelList = SysPostConvert.INSTANCE.entity2Excel(list);
//        //序号id
//        for (int i = 0; i < excelList.size(); i++) {
//            excelList.get(i).setIndex(i+1);
//        }
//        return util.exportExcel(excelList, "岗位管理");
//    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:post:query')")
    @GetMapping(value = "/{postId}")
    public AjaxResult getInfo(@PathVariable Long postId)
    {
        return AjaxResult.success(SysPostConvert.INSTANCE.entity2dto(postService.getById(postId)));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:add')")
    @SysLog(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysPostDTO postDTO)
    {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(postDTO)))
        {
            return AjaxResult.error("新增岗位'" + postDTO.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(postDTO)))
        {
            return AjaxResult.error("新增岗位'" + postDTO.getPostName() + "'失败，岗位编码已存在");
        }
        postDTO.setCreateBy(SecurityUtils.getUsername());
        log.info("新增的岗位信息：{}",postDTO);
        return toAjax(postService.save(SysPostConvert.INSTANCE.dto2entity(postDTO)));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:edit')")
    @SysLog(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysPostDTO postDTO)
    {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(postDTO)))
        {
            return AjaxResult.error("修改岗位'" + postDTO.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(postDTO)))
        {
            return AjaxResult.error("修改岗位'" + postDTO.getPostName() + "'失败，岗位编码已存在");
        }
        postDTO.setUpdateBy(SecurityUtils.getUsername());
        log.info("修改的岗位信息：{}",postDTO);
        return toAjax(postService.updateById(SysPostConvert.INSTANCE.dto2entity(postDTO)));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:post:remove')")
    @SysLog(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public AjaxResult remove(@PathVariable List<Long>postIds)
    {
        return toAjax(postService.removeByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysPost> posts = postService.list();
        return AjaxResult.success(posts);
    }
}
